package sddz.foodmanage.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.List;

import sddz.foodmanage.model.CommodityBean;
import sddz.foodmanage.model.UserStatusBean;


/**
 * Created by zhanglei on 2017/2/4.
 * 用于储存一些简单的数值
 */
public class SpfUtils {

//    public static LoginBean bean;
//    public static GestureBean gestureBean;
    public int miao;

    /**
     * 储存token
     *
     * @param context
     */
    public static void savetoken(Context context, String token) {
        SharedPreferences preferences = context.getSharedPreferences("token", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("token", token);
        edit.commit();
    }
    /**
     * 储存选择好的商品
     *
     * @param context
     */
    public static void setshangpin(Context context, String databeanlist) {
        SharedPreferences preferences = context.getSharedPreferences("databeanlist", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("databeanlist", databeanlist);

            edit.commit();

    }
    /**
     * 储存待评论数据
     *
     * @param context
     */
    public static void setwaitpl(Context context, String databeanlist) {
        SharedPreferences preferences = context.getSharedPreferences("waitpl", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("waitpl", databeanlist);
        edit.commit();

    }
    /**
     获取待评论
     *
     * @param context
     */
    public static String getwaitpl(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("waitpl", Context.MODE_PRIVATE);
        String token = preferences.getString("waitpl", "");
        return token;

    }
    /**
     * 清空掉待评论数据
     */
    public static void clearwaitpl(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("waitpl", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }

    /**
     获取选择好的商品
     *
     * @param context
     */
    public static String getshangpin(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("databeanlist", Context.MODE_PRIVATE);
        String token = preferences.getString("databeanlist", "");
        return token;

    }
    /**
     * 清空掉选好的商品
     */
    public static void clearshangpin(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("databeanlist", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }


    /**
     * 获取token
     * @param context
     */
    public static String gettoken(Context context) {
        //获取数据
        SharedPreferences preferences = context.getSharedPreferences("token", Context.MODE_PRIVATE);
        String token = preferences.getString("token", "");
//
//        //赋值数据
//        UserStatusBean.DataBean bean = new UserStatusBean.DataBean();
//        bean.setToken(token);

        return token;
    }
    public static void clearUserData(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
    public static void clearToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("token", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
    /**
     * 储存用户账号信息
     *
     * @param context
     * @param uBean
     */
    public static void setUserData(Context context, UserStatusBean.DataBean.UserBean uBean) {
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
   if(uBean!=null) {
    edit.putInt("userid", uBean.getUser_id());
    edit.putString("name", uBean.getName());
    edit.putString("avatar", uBean.getAvatar());
    edit.putInt("type", uBean.getType());
    edit.putInt("dinner_num",uBean.getDinner_num());
    edit.commit();
}
    }

    /**
     * 获取用户的账号和密码
     *
     * @param context
     * @return
     */
    public static UserStatusBean.DataBean.UserBean getUserData(Context context) {
        //获取数据
        SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        int userid = preferences.getInt("userid", 0);
        String name = preferences.getString("name", "");
        String avatar = preferences.getString("avatar", "");
        int type = preferences.getInt("type", 0);
        int dinner_num = preferences.getInt("dinner_num", 0);


        //赋值数据
        UserStatusBean.DataBean.UserBean bean = new UserStatusBean.DataBean.UserBean();
        bean.setUser_id(userid);
        bean.setName(name);
        bean.setAvatar(avatar);
        bean.setType(type);
        bean.setDinner_num(dinner_num);

        return bean;
    }





    /**
     * 设置是否登录
     *
     * @param context
     * @param isLogin
     */
    public static void setLogin(Context context, boolean isLogin) {
        SharedPreferences preferences = context.getSharedPreferences("isLogin", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean("isLogin", isLogin);
        edit.commit();
    }

    /**
     * 获取是否登录
     *
     * @param context
     * @return
     */
    public static boolean isLogin(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("isLogin", Context.MODE_PRIVATE);
        return preferences.getBoolean("isLogin", false);
    }



    /**
     * 极光推送获得数据
     * @param context
     * @param regId
     */
    public static void setRegId(Context context, String regId) {
        SharedPreferences sp = context.getSharedPreferences("regId", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("regId", regId);
        edit.commit();
    }

    public static String getRegId(Context context) {
        SharedPreferences sp = context.getSharedPreferences("regId", Context.MODE_PRIVATE);
        return sp.getString("regId", "");
    }


}
